package com.hh.balance.action;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hh.balance.dto.UserBalanceDTO;
import com.hh.balance.dto.UserBalanceRecordDTO;
import com.hh.balance.entity.Balance;
import com.hh.balance.entity.BalanceRecord;
import com.hh.balance.entity.User;
import com.hh.balance.service.BalanceRecordService;
import com.hh.balance.service.BalanceService;
import com.hh.balance.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author tiger
 * @version 1.0
 * @data 2022/10/20 11:51
 */
@RequestMapping("/hh/v1")
@RestController
public class BalanceAction {

    @Autowired
    BalanceService balanceService;

    @Autowired
    UserService userService;

    @Autowired
    BalanceRecordService balanceRecordService;

    private final static Logger LOGGER= LoggerFactory.getLogger(BalanceAction.class);

    /**
     * 获取用户余额
     * @param userId
     * @return
     */
    @GetMapping(value = "/getAll/{userId}")
    public UserBalanceDTO getBalance(@PathVariable int userId) {
        User user = userService.getById(userId);
        Balance balance = balanceService.getById(user.getBalanceId());
        UserBalanceDTO userBalanceDTO = new UserBalanceDTO(user,balance);
        return userBalanceDTO;
    }

    /**
     * 50元购买
     * @param userId
     * @param ammount
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/comsume/{userId}/{ammount}")
    public String comsume(@PathVariable int userId,@PathVariable Double ammount) throws Exception {
        try {
            //查询用户余额
            User user = userService.getById(userId);
            //获取用户余额
            Balance balance = balanceService.getById(user.getBalanceId());
            Double userAmmount = balance.getBalance();
            if(userAmmount > ammount){
                long times = System.currentTimeMillis();
                BalanceRecord balanceRecord = new BalanceRecord();
                balanceRecord.setBalanceId(balance.getId());
                balanceRecord.setBalanceRecord((-1.0D)*ammount);
                balanceRecord.setChangeTime(String.valueOf(times));
                userAmmount = userAmmount-ammount;
                balance.setBalance(userAmmount);
                balanceService.updateById(balance);
                balanceRecordService.save(balanceRecord);
            }else{
                return "余额不足";
            }
        }catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
        return "购买成功";
    }

    /**
     * 退款
     * @param userId
     * @param ammount
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/refund/{userId}/{ammount}")
    public String refund(@PathVariable int userId,@PathVariable Double ammount) throws Exception {
        //查询用户余额
        try{
            User user = userService.getById(userId);
            Balance balance = balanceService.getById(user.getBalanceId());
            balance.setBalance(balance.getBalance()+ammount);
            //退款记录
            BalanceRecord balanceRecord = new BalanceRecord();
            balanceRecord.setBalanceId(balance.getId());
            balanceRecord.setBalanceRecord(ammount);
            long times = System.currentTimeMillis();
            balanceRecord.setChangeTime(String.valueOf(times));
            //退款更新
            balanceService.updateById(balance);
            //插入退款记录
            balanceRecordService.save(balanceRecord);
        }catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
        return "退款成功";
    }

    /**
     * 退款
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/detail/{userId}")
    public UserBalanceRecordDTO detail(@PathVariable int userId) throws Exception {

        UserBalanceRecordDTO userBalanceRecordDTO = new UserBalanceRecordDTO();
        //查询用户余额
        try{
            User user = userService.getById(userId);
            Balance balance = balanceService.getById(user.getBalanceId());
            LambdaQueryWrapper<BalanceRecord> balanceRecordIn = Wrappers.<BalanceRecord> lambdaQuery().in(BalanceRecord::getBalanceId, balance.getId());
            List<BalanceRecord> list = balanceRecordService.list(balanceRecordIn);
            userBalanceRecordDTO.setUser(user);
            userBalanceRecordDTO.setBalance(balance);
            userBalanceRecordDTO.setBalanceRecord(list);
        }catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
        return userBalanceRecordDTO;
    }
}
